<html>
<head>
<link href="css/formulario.css" rel="stylesheet" type="text/css">	
</head>
</html>
<?php require_once('Connections/local.php'); ?>
<?php

session_start();
if (!isset($_SESSION['MM_COD_USUARIO']))
    header("location:login.php");


if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (	(isset($_POST["MM_insert"]) && ($_POST["MM_insert"] == "form")) ){
   $query="select * from reserva where COD_HORARIO = ".$_POST['COD_HORARIO']. "AND COD_PISTA = ".$_POST['COD_PISTA']." AND fecha = ".$_POST['fecha'];

$comprobar= mysql_query($query);

	if (mysql_num_rows ($comprobar)>0){
	 header ("Location: falloreserva.php");
	var_dump($query);
	  }else{
  $insertSQL = sprintf("INSERT INTO reserva (observaciones, COD_USUARIO, COD_PISTA, COD_HORARIO, fecha, luz) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['observaciones'], "text"),
					   GetSQLValueString($_SESSION['MM_COD_USUARIO'], "int"),
                       GetSQLValueString($_POST['COD_PISTA'], "int"),
                       GetSQLValueString($_POST['COD_HORARIO'], "int"),
					   GetSQLValueString($_POST['fecha'], "date"),
					   GetSQLValueString($_POST['luz'], "text"));
  mysql_select_db($database_local, $local);
  $Result1 = mysql_query($insertSQL, $local) or die(mysql_error());

  $insertGoTo = "misreservas.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
}  
  header(sprintf("Location: %s", $insertGoTo));

  }
 }
?>
<html>

<body>
<div id="stylized" class="myform"> 
<form id="form" name="form" action="<?php echo $editFormAction; ?>" method="POST">
<h1>Haz Tu Reserva</h1>
	 <br /><br />
    <label>Observaciones</label>
 <br> <textarea name="observaciones" rows="4" cols="45">
 
</textarea>
  <br /><br />
   <label> PISTA</label>
 
  <select name="COD_PISTA"> 
    <?php 
	mysql_select_db($database_local, $local);
	$cons= mysql_query("select * from pista");
	echo var_dump($cons);		
	while ($row=mysql_fetch_array($cons)){
	 echo "<option value=\"".$row['COD_PISTA']."\">".$row['NOMBRE_P']."</option>"; 
      }?>
  </select>
  <br /><br />
    
    <label>HORARIO</label>
  <select name="COD_HORARIO">
  <?php 
  mysql_select_db($database_local, $local);
  $consulta= mysql_query("select * from horario");
 
  while ($row=mysql_fetch_array($consulta)){
	 echo "<option value=\"".$row['COD_HORARIO']."\">".$row['HORARIO']."</option>"; 
      };

		  ?>
      </select>
	  
  				 
  <br /><br />
  <label>Fecha</label>
  <input name="fecha" type="date" value="<?php echo date('Y-m-d'); ?>"/></p>
   <br /><br /><label>Luz</label>
 <select name="luz">
    <option value="Si">Si</option>
	<option value="No">No</option>
      </select>
   <br /><br />
    <input type="submit" name="submit" value="ENVIAR" />
    <input type="hidden" name="MM_insert" value="form" />
  
</form>

</body>
</html>

